<!--
 * @Author: ningjz
 * @Date: 2022-03-10 17:11:51
 * @Description: 猪粮比
-->

<template>
  <com-box :title="title">
    <template v-slot>
      <loading v-show="loading" />
      <div @click="handZLBClick(!isZlb)" class="zlb">
        <div class="zlb-ri">
          <div class="ri-num">期号 : {{ objData.EXENO }}</div>
          <div class="ri-img" :class="{ 'img-back': isZlb }">
            <img src="../img/zlb-zhu.png" />
          </div>
        </div>
        <div class="zlb-le">
          <div class="le-top">
            <div
              class="top-warn-style"
              :class="{
                'top-warn1': objData.warnLevel === '1',
                'top-warn2': objData.warnLevel === '2',
                'top-warn3': objData.warnLevel === '3'
              }"
            >
              <div class="warn-hua" v-if="imgHua[0]">
                <img v-if="isWarn" :src="imgHua[0]" />
                <img v-else :src="imgHua[1]" />
              </div>
              <div class="warn-img" />
            </div>
            <div class="top-til">
              {{ objData.goodsName }} : &nbsp;<span
                >{{ objData.avgPrice }}
                <span v-if="objData.avgPrice">:&nbsp;1</span></span
              >
            </div>
          </div>
          <div class="le-btm">
            <div
              v-for="(item, index) in objData.data"
              :key="index"
              class="btm-name1 btm-item"
            >
              <span>{{ item.goodsName }} ：</span>
              <span>{{ item.avgPrice }} {{ item.unit }}</span>
            </div>
          </div>
        </div>
      </div>
    </template>
  </com-box>
</template>
<script lang="ts">
import { defineComponent, reactive, toRefs, onBeforeUnmount, watch } from 'vue'
import httpRequest from '@/service'
import loading from '@/charts/loading.vue'
import comBox from '@/components/comBox/comBox.vue'
interface IState {
  title: string
  objData: any
  isWarn: boolean
  imgHua: any[]
  items: any
  loading: boolean
  isZlb: boolean
  getData: (adcode: string) => void
  handWarnSetInterval: () => void
}
export default defineComponent({
  name: 'rYfjgqs',
  components: {
    comBox,
    loading
  },
  props: {
    adcode: {
      type: String,
      default: ''
    },
    isJszsBars: {
      type: Boolean,
      default: false
    }
  },
  setup(props, { emit }) {
    const state: IState = reactive({
      title: '',
      objData: {},
      isWarn: true,
      imgHua: [],
      loading: false,
      items: null,
      isZlb: false,
      getData: (adcode: string) => {
        state.loading = true
        httpRequest
          .get({ url: 'M2P1Zlb', params: { adcode } })
          .then((res) => {
            if (res.code === 200) {
              state.title = res.title
              state.objData = {
                warnLevel: res.warnLevel,
                goodsName: res.goodsName,
                goodsCode: res.goodsCode,
                EXENO: res.EXENO,
                avgPrice: res.avgPrice,
                data: res.data
              }
              if (res.warnLevel !== '0') {
                state.imgHua[0] = require(`../img/zlb-hua${res.warnLevel}-1.png`)
                state.imgHua[1] = require(`../img/zlb-hua${res.warnLevel}-2.png`)
              }
            }
            state.loading = false
          })
          .catch(() => {
            state.loading = false
            console.log('请求失败')
          })
      },
      handWarnSetInterval: () => {
        state.items = setInterval(() => {
          state.isWarn = !state.isWarn
        }, 200)
      }
    })
    const handZLBClick = (bool: boolean) => {
      state.isZlb = bool
      let ids = ''
      if (state.isZlb) {
        const arr = [
          state.objData.goodsCode,
          state.objData.data[0].goodsCode,
          state.objData.data[1].goodsCode
        ]
        ids = arr.join()
      } else {
        ids = ''
      }
      emit('change', ids, state.isZlb)
    }
    onBeforeUnmount(() => {
      clearInterval(state.items)
    })
    state.handWarnSetInterval()
    watch(
      () => props.adcode,
      (newVal) => {
        state.getData(newVal)
      }
    )
    watch(
      () => props.isJszsBars,
      (newVal) => {
        state.isZlb = newVal
        if (newVal === false) {
          handZLBClick(newVal)
        }
      }
    )
    const { title, isWarn, objData, isZlb, imgHua, loading } = toRefs(state)
    return {
      title,
      objData,
      imgHua,
      isWarn,
      isZlb,
      loading,
      handZLBClick
    }
  }
})
</script>
<style lang="less" scoped>
.zlb {
  display: flex;
  width: 100%;
  height: 100%;
  cursor: pointer;
  .zlb-ri {
    display: flex;
    flex-direction: column;
    width: 30%;
    padding: 10px 0px 5px 0px;
    align-items: center;
    .ri-num {
      font-size: 12px;
      font-weight: bold;
      color: #00edf0;
    }
    .ri-img {
      width: 125%;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .img-back {
      background: url('../img/zlb-gq.png') no-repeat;
      background-size: 100% 110%;
    }
  }
  .zlb-le {
    width: 70%;
    height: 100%;
    padding: 5px 11px;
    .le-top {
      display: flex;
      height: 50%;
      .top-warn-style {
        width: 80px;
        position: relative;
        right: 8px;
        .warn-hua {
          img {
            width: 100%;
          }
          width: 54px;
          height: 23px;
        }
        .warn-img {
          bottom: 31px;
          position: relative;
          right: 19px;
          height: 132%;
          width: 114%;
        }
      }
      .top-warn1 {
        .warn-img {
          background: url()
            no-repeat;
          background-size: 125% 100%;
        }
      }
      .top-warn2 {
        .warn-img {
          background: url()
            no-repeat;
          background-size: 125% 100%;
        }
      }
      .top-warn3 {
        .warn-img {
          background: url()
            no-repeat;
          background-size: 125% 100%;
        }
      }
      .top-til {
        display: flex;
        align-items: center;
        justify-content: flex-end;
        width: 100%;
        font-size: 31px;
        font-weight: bold;
        color: #ef7809;
        white-space: nowrap;
        span {
          font-weight: 500;
          font-family: 'AliHYAiHei';
        }
      }
    }
    .le-btm {
      width: 100%;
      height: 50%;
      font-size: 15px;
      font-weight: bold;
      display: flex;
      flex-direction: column;
      justify-content: flex-end;
      padding-bottom: 12px;
      .btm-item {
        color: #09fff6;
        display: flex;
        margin: 2px 0px;
        justify-content: space-between;
        span:nth-child(2) {
          font-weight: 500;
          font-family: 'AliHYAiHei';
        }
      }
    }
  }
}
</style>
